package com.hmsm.sys.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.hmsm.server.base.BaseAction;
import com.hmsm.server.util.Util;
import com.hmsm.sys.po.SysConfig;
import com.hmsm.sys.po.UserInfo;
import com.hmsm.sys.service.SysConfigService;

@SuppressWarnings("serial")
public class SysConfigAction extends BaseAction<UserInfo>{

	@Resource
	private SysConfigService sysConfigService;
	
	public SysConfigService getSysConfigService() {
		return sysConfigService;
	}

	public void setSysConfigService(SysConfigService sysConfigService) {
		this.sysConfigService = sysConfigService;
	}

	private List<SysConfig> sysConfigs;

	public List<SysConfig> getSysConfigs() {
		return sysConfigs;
	}

	public void setSysConfigs(List<SysConfig> sysConfigs) {
		this.sysConfigs = sysConfigs;
	}
	
	private String[] sysConfigMap;

	public String[] getSysConfigMap() {
		return sysConfigMap;
	}

	public void setSysConfigMap(String[] sysConfigMap) {
		this.sysConfigMap = sysConfigMap;
	}

	public SysConfigAction(){
		
	}
	
	public String getSysConfigInfo(){
		return SUCCESS;
	}
	
	public String manage(){
		sysConfigs = getSysConfigService().getSysConfigDao().query(new SysConfig());
		for(SysConfig sysConfig: sysConfigs){
			String title = sysConfig.getName();
			String remark = sysConfig.getRemark();
			if(remark!=null&&remark.length()>0){
				// 必填标记
				if (remark.startsWith("*")){
					sysConfig.setRequired(true);
					remark = remark.substring(1).trim();
				}
				
				if(remark.indexOf(':')>2){
					title = remark.substring(0, remark.indexOf(':'));
					remark = remark.substring(remark.indexOf(':')+1);
				}

				// 下拉选项
				String spChar = null; 
				String[] dd = new String[]{"1.","0.","1:","0:"};
				for(String bb:dd){
					if (remark.startsWith(bb)){
						spChar = bb;
						break;
					}
				}
				if (spChar!=null){
					List<Map<String, String>> list = new ArrayList<Map<String,String>>();
					
					spChar = spChar.substring(1);
					String[] cc;
					if (".".equals(spChar)){
						cc = remark.split("\\.");
					}else{
						cc = remark.split(spChar);
					}
					String nextValue = cc[0].substring(0, 1);
					Map<String, String> item = null;
					for(int i=0;i<cc.length-1;i++){
						String s = cc[i+1];
						String value = nextValue;
						String text;
						if (i==cc.length-2){
							text = s.trim();
						}else{
							text = s.substring(0,s.length()-1).trim();
							nextValue = s.substring(s.length()-1);
						}
						if (text.endsWith(";")){
							text = text.substring(0, text.length()-1).trim();
						}
						item = new HashMap<String, String>();
						item.put("value", value);
						item.put("text", text);
						list.add(item);
					}
					
					sysConfig.setSelectList(list);
				}
			}
			
		}
		return SUCCESS;
	}

	/**
	 * 更新系统配置
	 * 
	 * @方法名 save 原名 LoginAction.updateSysConfig()
	 * @author zhangshuai 2013年12月3日 10:30:00
	 * @return
	 */
	public String save() {
		try {
			Util u = new Util();
			for (int i = 0; i < sysConfigMap.length; i++) {
				String[] config = u.getSplitComplex(sysConfigMap[i], "|");
				SysConfig sysConfig = getSysConfigService().getById(Integer.parseInt(config[0]));
				sysConfig.setValue(config[1]);
				getSysConfigService().update(sysConfig);
				sysConfig = null;
			}
			getSysConfigService().refreshSysConfig();
			this.setMessage("保存成功！");
		} catch (Exception e) {
			e.printStackTrace();
			setMessage("发生错误："+e);
			addActionError(getMessage());
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * 更新系统配置
	 * @return
	 */
	public String updateConfig() {
		UserInfo currentUser = getSessionUser();
		try {
			for (SysConfig sysConfig:sysConfigs) {
				getSysConfigService().updateByName(sysConfig, currentUser);
			}
			getSysConfigService().refreshSysConfig();
			this.setMessage("保存成功！");
			mapResult.put("success", true);
			mapResult.put("message", getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			setMessage("发生错误："+e);
			addActionError(getMessage());
			mapResult.put("success", false);
			mapResult.put("errorMessage", getMessage());
		}
		
		return "json";
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
